/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	public sealed partial class ChooseConceptualTemplateDialog : Form
	{
		#region Public Properties
		[Browsable(false)]
		public ConceptualTemplate SelectedTemplate
		{
			get
			{
				return (ConceptualTemplate) templatesListBox.SelectedItem;
			}
		}

		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public Guid Guid { get; private set; }

		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public string FileName
		{
			get
			{
				return PathFormat.EnsureFileName(fileName);
			}
			set
			{
				fileNameTextBox.Text = value;
			}
		}
		#endregion

		#region Private / Protected
		private static readonly char[] invalidFileNameCharacters = System.IO.Path.GetInvalidFileNameChars();
		private string fileName;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseConceptualTemplateDialog" /> class.
		/// </summary>
		public ChooseConceptualTemplateDialog(IEnumerable<ConceptualTemplate> templates)
			: this()
		{
			if (templates == null)
				throw new ArgumentNullException("templates");

			foreach (ConceptualTemplate template in templates)
				templatesListBox.Items.Add(template);

			if (templatesListBox.Items.Count > 0)
				templatesListBox.SelectedIndex = 0;

			GenerateGuid();
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseConceptualTemplateDialog" /> class.
		/// </summary>
		private ChooseConceptualTemplateDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		private void ShowPreview(ConceptualTemplate template)
		{
			templatePreviewWebBrowser.Url = (template == null) ? null : new Uri(template.FullPath);
		}

		private void GenerateGuid()
		{
			this.Guid = Guid.NewGuid();
			guidTextBox.Text = this.Guid.ToString("D");
		}
		#endregion

		#region Event Handlers
		private void okButton_Click(object sender, EventArgs e)
		{
			fileName = fileNameTextBox.Text.Trim();

			if (fileName.Length != 0 && ValidateChildren())
				DialogResult = DialogResult.OK;
		}

		private void templatesListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (templatesListBox.SelectedIndex > -1)
			{
				ConceptualTemplate template = (ConceptualTemplate) templatesListBox.SelectedItem;

				ShowPreview(template);

				string file = fileNameTextBox.Text.Trim();

				if (file.Length == 0 ||
					(file.StartsWith("New ", StringComparison.Ordinal) && file.EndsWith(" Topic", StringComparison.Ordinal)))
				{
					fileNameTextBox.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture, 
						Resources.Text.NewTopic, template.Name + " ");
				}

				okButton.Enabled = true;
			}
			else
				okButton.Enabled = false;
		}

		private void regenerateGuidButton_Click(object sender, EventArgs e)
		{
			GenerateGuid();
		}

		private void fileNameTextBox_TextChanged(object sender, EventArgs e)
		{
			okButton.Enabled = fileNameTextBox.Text.Trim().Length > 0 && templatesListBox.SelectedIndex > -1;
		}

		private void fileNameTextBox_Validating(object sender, CancelEventArgs e)
		{
			if (fileNameTextBox.Text.Length == 0 || fileNameTextBox.Text.IndexOfAny(invalidFileNameCharacters) > -1)
			{
				e.Cancel = true;
				errors.SetError(fileNameTextBox, Resources.Errors.InvalidTopicFileName);
			}
			else
				errors.SetError(fileNameTextBox, null);
		}
		#endregion
	}
}